<?php
namespace Admin\Service;
//商品类
use Admin\AppConst\WebConst;
use Admin\Model\MenuModel;

class MenuService extends BaseService
{

    /**
     * @var MenuService
     */
    private static $instance;

    /**
     * Get the instance of MenuService.
     * @return MenuService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new MenuService();
        }
        return self::$instance;
    }


    /**
     * @function    getList     //菜单数据
     * @param       $isShow     bool     是否验证isshow是否显示
     * @author      zhangle
     * @date        2018-07-26
     * @return      array
     */
    public function getListNotTrees(){
        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;

        //获取MenuId
        $list   = MenuModel::get()->getList();
        if(!empty($list)){
            $result = $list;
        }
        return $result;
    }

    /**
     * @function    getList         //菜单数据
     * @param       $menuIdList     array     已有的权限
     * @author      zhangle
     * @date        2018-07-26
     * @return      array
     */
    public function getListZTrees($menuIdList = WebConst::DEFAULT_TYPE_EMPTY_ARRAY){
        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        //获取MenuId
        $list   = MenuModel::get()->getList();
        if(!empty($list)){
            foreach ($list as $item) {
                $data   = array(
                    'id'    => $item['id'],
                    'pId'   => $item['pid'],
                    'name'  => $item['name'],
                    'open'  => true,
                );
                if($item['id'] == 1){
                    $data['checked']   = true;
                }
                if(in_array($item['id'], $menuIdList)){
                    $data['checked']   = true;
                }
                $result[]   = $data;
            }
        }

        return $result;
    }

    /**
     * @function    getList     //菜单数据
     * @param       $isShow     bool     是否验证isshow是否显示
     * @author      zhangle
     * @date        2018-07-26
     * @return      array
     */
    public function getList($isShow = WebConst::DEFAULT_TYPE_FALSE){
        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;

        //获取MenuId
        $list   = MenuModel::get()->getList($isShow);
        if(empty($list)){
            return $result;
        }

        $result = $this->getTree($list);

        return $result;
    }

    private function getTree($data, $pId = WebConst::CATEGORY_PID_DEFAULT)
    {
        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        foreach($data as $key => $val){
            //是否显示
            $val['isShowMsg']   = '<ins class="btn bg-orange btn-sm">不显示</ins>';
            if($val['is_show'] == WebConst::MENU_SHOW_TRUE){
                $val['isShowMsg']   = '<ins class="btn bg-success btn-sm">显示</ins>';
            }
            if($val['pid'] == $pId){ //父亲找到儿子
                $child      = $this->getTree($data, $val['id']);
                if(!empty($child)){
                    $val['child']  = $child;
                }
                $result[$val['id']]   = $val;
            }
        }
        return $result;
    }


    /**
     * @function    getListSelect     //菜单数据html
     * @param       $id    int     默认选中id
     * @author      zhangle
     * @date        2018-07-26
     * @return      string
     */
    public function getListSelect($id = WebConst::CATEGORY_PID_DEFAULT){
        $result = WebConst::DEFAULT_TYPE_EMPTY;
        //获取Menu
        $list   = MenuModel::get()->getList();
        if(empty($list)){
            return $result;
        }

        $listTree = $this->getTree($list);

        //下拉处理
        $result   = $this->getTreeSelect($listTree, $id);
        return $result;
    }

    /**
     * @function    getTreeSelect   递归显示 select  html
     * @param       $data   array   需要处理的数据
     * @param       $id     int     默认选中的id
     * @param       $html   string  生成的html
     * @param       $p      int     分类的级别
     * @author      zhangle
     * @date        2018-07-26
     * @return      string
     */
    private function getTreeSelect($data, $id, $html = WebConst::DEFAULT_TYPE_EMPTY, $p = WebConst::CATEGORY_PID_DEFAULT)
    {
        $p++;
        switch ($p){
            case 2:
                $prefix = '|_________';
                break;
            case 3:
                $prefix = '|__________________';
                break;
            default:
                $prefix = '';
                break;
        }
        $html = WebConst::DEFAULT_TYPE_EMPTY;
        foreach ($data as $value) {
            $checked    = '';
            if($value['id'] == $id){
                $checked    = 'selected';
            }
            $html   .= '<option value="'.$value['id'].'" '.$checked.'>'.$prefix.$value['name'].'</option>';
            if($value['child']){
                $html   .= $this->getTreeSelect($value['child'], $id, $html, $p);
            }
        }
        return $html;
    }


    /**
     * @function    createMenu         创建
     * @param       $pid        int     分类父级id
     * @param       $iconName   string  bootstrap 提供的 icon
     * @param       $name       string  菜单名称
     * @param       $sort       int     排序
     * @param       $url        string  跳转url
     * @param       $isShow     int     是否显示
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function createMenu($pid, $iconName, $name, $sort, $url, $isShow){
        return MenuModel::get()->createMenu($pid, $iconName, $name, $sort, $url, $isShow);
    }

    /**
     * @function    editMenu    更新
     * @param       $id         int     id
     * @param       $pid        int     分类父级id
     * @param       $iconName   string  bootstrap 提供的 icon
     * @param       $name       string  菜单名称
     * @param       $sort       int     排序
     * @param       $url        string  跳转url
     * @param       $isShow     int     是否显示
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function editMenu($id, $pid, $iconName, $name, $sort, $url, $isShow){
        return MenuModel::get()->editMenu($id, $pid, $iconName, $name, $sort, $url, $isShow);
    }

    /**
     * @function    getMenuById         查询单个
     * @param       $id         int     id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function getMenuById($id){
        return MenuModel::get()->getMenuById($id);
    }

    /**
     * @function    getListByPid 根据pid查询数据
     * @param       $pid         int     pid
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function getListByPid($pid){
        return MenuModel::get()->getListByPid($pid);
    }

    /**
     * @function    deleteMenu  查询单个
     * @param       $id         int     id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function deleteMenu($id){
        return MenuModel::get()->deleteMenu($id);
    }

    /**
     * @function    deleteMenu  查询单个
     * @param       $url        string  url
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function getMenuIdByUrl($url){
        return MenuModel::get()->getMenuIdByUrl($url);
    }
}